package sina.girl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetPic {
    
    private static final Log log = LogFactory.getLog(GetPic.class);
    
    private static HttpClient client = null;
    
    public GetPic(HttpClient _client) {
        client = _client;
    }
    
    /***
     *思路：
     *1：根据图集汇总抓取到每个图集的地址
     *2：进入图集，找到当前图片的地址
     *3：根据当前地址，匹配下一页的地址(下一页不是无限的，根据图集大小判断下一页)
     *4：进入下一页，找到当前图片地址
     *5：下载图片
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public List<List<Map<String, String>>> getPicInfos(List<String> urlList) throws ClientProtocolException,
            IOException {
        log.info("getPicInfos start");
        List<List<Map<String, String>>> picList = new ArrayList<List<Map<String, String>>>(0);
        
        if (null != urlList && urlList.size() > 0) {
            for (String url : urlList) {
                List<String> picPathList = getPicList(url);//图集地址
                if (null != picPathList && picPathList.size() > 0) {
                    for (String tmp : picPathList) {
                        picList.add(getPicInfo(tmp));
                    }
                }
            }
        }
        log.info("getPicInfos end");
        return picList;
    }
    
    /**
     * 根据图集汇总抓取到每个图集的地址
     * @throws ClientProtocolException
     * @throws IOException
     */
    public List<String> getPicList(String url) throws ClientProtocolException, IOException {
        log.info("getPic() start");
        List<String> picList = new ArrayList<String>(0);
        HttpPost post = new HttpPost(url);
        HttpResponse resonse = client.execute(post);
        if (resonse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = resonse.getEntity();
            if (entity != null) {
                String html = EntityUtils.toString(entity);
                html = new String(html.getBytes("ISO-8859-1"), "gbk");
                log.info("html:" + html);
                Document doc = Jsoup.parse(html, "utf-8");
                Elements elements = doc.select("table[style=height:180px] tr td a");
                //				Elements picListName = doc.select("h2 style=[line-height: 22px;] a");
                //				log.info("elements:"+elements);
                if (null != elements && elements.size() > 0) {
                    for (int i = 0; i < elements.size(); i++) {
                        Element element = elements.get(i);
                        String src = element.attr("href");//每个图集的地址
                        picList.add(i, src);
                    }
                }
            }
        }
        log.info("getPic() end");
        return picList;
    }
    
    /**
     * 根据一个图集的url，就可以获取整个图集图片的地址
     * @param picUrl
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public List<String> getEveryPicListLinks(String url) throws ClientProtocolException, IOException {
        List<String> list = new ArrayList<String>(0);
        HttpPost post = new HttpPost(url);
        HttpResponse resonse = client.execute(post);
        if (resonse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = resonse.getEntity();
            if (entity != null) {
                String html = EntityUtils.toString(entity);
                html = new String(html.getBytes("ISO-8859-1"), "gbk");
                //				log.info(html);
                Document doc = Jsoup.parse(html, "utf-8");
                Elements elements = doc.select("div img");
                if (null != elements && elements.size() > 0) {
                    for (Element element : elements) {
                        list.add(element.attr("src"));
                    }
                }
                
            }
        }
        return list;
    }
    
    /**
     * 获取图片信息
     * pacname:图集名称	picpath：图集路径
     * @param url 图集路径
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    @SuppressWarnings("unused")
    public List<Map<String, String>> getPicInfo(String url) throws ClientProtocolException, IOException {
        List<Map<String, String>> picList = new ArrayList<Map<String, String>>(0);
        Map<String, String> map = null;
        HttpPost post = new HttpPost(url);
        HttpResponse resonse = client.execute(post);
        if (resonse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = resonse.getEntity();
            if (entity != null) {
                String html = EntityUtils.toString(entity);
                html = new String(html.getBytes("ISO-8859-1"), "gbk");
                //				log.info(html);
                Document doc = Jsoup.parse(html, "utf-8");
                Elements picPathElements = doc.getElementsByTag("dl");// 查询图片路径
                
                if (null != picPathElements && picPathElements.size() > 0) {
                    for (Element element : picPathElements) {
                        map = new HashMap<String, String>();
                        String strs[] = element.text().split(" ");
                        map.put("picname", strs[0]);
                        map.put("picpath", strs[1]);
                        picList.add(map);
                    }
                }
            }
        }
        return picList;
    }
    
}